package de.widas.gaesf.cron;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import de.widas.gaesf.service.EintragManager;

/**
 * Created on: 19.10.2009, 13:43:23<br/>
 * Copyright 2009 by WidasConcepts GmbH<br/>
 * 
 * @author Meykel Gruel
 */
public class CronJobServlet extends HttpServlet {

	private static final long serialVersionUID = 2690630076507557414L;

	private EintragManager persistenceManager;
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		if (getEintragManager() != null) {
			getEintragManager().removeAlle();
			resp.setStatus(HttpServletResponse.SC_OK);
		} else {
			resp.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
		}
	}

	private EintragManager getEintragManager() {
		if (persistenceManager == null) {
			ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
			persistenceManager = (EintragManager) context.getBean("persistenceManager");
		}
		return persistenceManager;
	}
	
}
